package com.controller;

import cn.edu.scau.biubiusuisui.annotation.FXController;
import cn.edu.scau.biubiusuisui.annotation.FXReceiver;
import cn.edu.scau.biubiusuisui.annotation.FXWindow;
import cn.edu.scau.biubiusuisui.entity.FXBaseController;
import com.util.PathUtil;
import javafx.concurrent.Worker;
import javafx.fxml.FXML;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import netscape.javascript.JSObject;

import java.net.URL;
import java.util.Map;

/*
 * @author: Ricky
 * @date: 2020-8-15 14:01
 */
@FXController(path = "fxml/Seconed.fxml")
@FXWindow(title = "投屏窗口", style = StageStyle.UNDECORATED)
public class SecondaryController extends FXBaseController {
    @FXML
    WebView webView;

    WebEngine engine;

    @Override
    public void initialize() throws Exception {
        engine = webView.getEngine();
        URL url = PathUtil.getURL("html/tp.html");
        engine.getLoadWorker().stateProperty().addListener((ov, oldState, newState) -> {
            if (newState == Worker.State.SUCCEEDED) {
                JSObject win = (JSObject) engine.executeScript("window");
                win.setMember("java", this);
            }
        });

        engine.load(url.toString());
    }

    @Override
    public void onShow() throws Exception {
        Map<String, Object> query = this.getQuery();
        for (String s : query.keySet()) {
            System.out.println("query-->" + s + ":" + query.get(s));
        }
        Map<String, Object> param = this.getParam();
        for (String s : param.keySet()) {
            System.out.println("param-->" + s + ":" + param.get(s));
        }
        System.out.println("SecondaryController onShow");
//        Stage stage = getStage();
//        stage.close();
    }

    @Override
    public void onHide() throws Exception {
        System.out.println("SecondaryController onHide");
    }

    /**
     * 接收者必须指定要订阅的[发送者类名:方法名]
     * 发送函数的返回值会注入到接收函数的参数中
     *
     * @param msg
     */
    @FXReceiver(name = "PrimaryController:sendMsg")
    public void FXReceiver(String msg) {
        // 处理导航栏的点击事件
        engine.executeScript("receiveMsg(" + msg + ")");
    }
}
